package com.example.cloud.controller.system;

import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.system.Role;
import com.example.cloud.model.system.User;
import com.example.cloud.service.system.RoleService;
import com.example.cloud.service.system.UserService;
import com.example.cloud.utils.ResultUtil;
import org.beetl.sql.core.SQLManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/1/26.
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    SQLManager sqlManager;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model){
        return this.singleQuery(model, Role.class);
    }

    @PostMapping("/saveOrUpdate")
    public void checkLogin(@RequestBody Role role) {
        roleService.doSave(role);
    }

    @GetMapping("/getUserRole/{userId}")
    public Result getUserRole(@PathVariable Long userId){
        Map<String,Object> params = new HashMap<String,Object>();
        User user = userService.findOneFromCache(userId);
        // 用户拥有的角色
        List<Role> userRoles = user.getRoles();
        // 还未分配的角色
        List<Long> userIds = new ArrayList<Long>();
        for (Role userRole : userRoles) {
            userIds.add(userRole.getId());
        }
        List<Role> allRoles = roleService.findAll();
        for (Role allRole : allRoles) {
            if(userIds.contains(allRole.getId())){
                allRole.setTodoitem(true);
            }
        }
        params.put("userRoles", userRoles);
        params.put("allRoles", allRoles);
        return ResultUtil.success(user, params);
    }

    @PostMapping("/addRole")
    public void addRole(@RequestBody Map params){
        Long userId = Long.valueOf(params.get("userId").toString());
        Long roleId = Long.valueOf(params.get("roleId").toString());
        Boolean todoitem = Boolean.valueOf(params.get("todoitem").toString());
        User user = userService.findOneFromCache(userId);
        List<Role> userRoles = user.getRoles();
        if(todoitem){
            // 添加
            Role role = roleService.findOneFromCache(roleId);
            if(role != null){
                userRoles.add(role);
            }
        }else{
            // 删除
            for (Role userRole : userRoles) {
                if(roleId.equals(userRole.getId())){
                    userRoles.remove(userRole);
                    break;
                }
            }
        }
        userService.doSave(user);
    }
}
